﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Visa.DB;
using System.Data;

namespace Visa.Service
{
    public class QuocGiaService
    {
        private VisaEntities db = new VisaEntities();

        public void AddOrUpdateQuocGia(QuocGia entity)
        {
            db.Entry(entity).State = entity.Id == -1 ? EntityState.Added : EntityState.Modified;
            db.SaveChanges();
        }

        public void DeleteQuuocGia(int id)
        {
            var c = db.QuocGias.SingleOrDefault(o => o.Id == id);
            c.Detele = true;
            db.SaveChanges();
        }

        public void DeleteChauLuc(int id)
        {
            var c = db.ChauLucs.SingleOrDefault(o => o.Id == id);
            c.Detele = true;
            db.SaveChanges();
        }

        public void AddOrUpdateChauLuc(ChauLuc entity)
        {
            db.Entry(entity).State = entity.Id == -1 ? EntityState.Added : EntityState.Modified;
            db.SaveChanges();
        }

        public ChauLuc getChauLucById(int id)
        {
            try
            {
                return db.ChauLucs.SingleOrDefault(o => o.Id == id);
            }
            catch { return null; }
        }

        public ChauLuc GetChauLucByCode(string code)
        {
            try
            {
                return db.ChauLucs.SingleOrDefault(o => o.regioncode == code);
            }
            catch { return null; }
        }

        public QuocGia getQuocGiaById(int id)
        {
            try
            {
                return db.QuocGias.SingleOrDefault(o => o.Id == id);
            }
            catch { return null; }
        }

        public List<ChauLuc> GetListChauLuc()
        {
            try
            {
                return db.ChauLucs.Where(o => o.Detele == false).OrderByDescending(l => l.DateCreate).ToList();
            }
            catch { return null; }
        }

        public List<QuocGia> GetListQuocGia(string code)
        {
            try
            {
                if (code != string.Empty)
                {
                    return db.QuocGias.Where(o => o.regioncode == code && o.Detele == false).OrderBy(l => l.Name).ToList();
                }
                else
                {
                    return db.QuocGias.Where(o => o.Detele == false).OrderBy(l => l.Name).ToList();
                }
            }
            catch { return null; }
        }

        public List<QuocGia> GetQuocGiaByCode(string Code)
        {
            try
            {
                return db.QuocGias.Where(o => o.regioncode == Code && o.Detele == false).OrderBy(l => l.Name).ToList();
            }
            catch { return null; }
        }
    }
}
